package com.haierp.controller;

import java.util.List;

import org.eclipse.jetty.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.haierp.model.Brand;
import com.haierp.service.IBrandService;
import com.haierp.util.JsonPageResult;
import com.haierp.util.JsonResult;
import com.haierp.vo.BrandQueryVO;
import com.wangzhixuan.commons.base.BaseController;
import com.wangzhixuan.commons.shiro.ShiroUser;

@Controller
@RequestMapping("/item/brand")
public class BrandController extends BaseController {

	@Autowired
	private IBrandService brandService;

	/**
	 * 添加品牌
	 *
	 * @param
	 * @return
	 */
	@RequestMapping("/add")
	@ResponseBody
	public Object add(Brand brand) {
		JsonResult<Brand> result = new JsonResult<>();
		if (brand.getId() != null) {
			return result.buildMsg("新增品牌ID有值").buildIsSuccess(false);
		}
		if(StringUtil.isBlank(brand.getName())) {
			return result.buildMsg("英文品牌不能为空").buildIsSuccess(false);
		}
		ShiroUser shiroUser = this.getShiroUser();
		brand.setCompanyId(shiroUser.getCompanyId());
		brandService.addBrand(brand);
		return result.buildIsSuccess(true);
	}
	
	/**
	 * 根据id查找品牌
	 *
	 * @param
	 * @return
	 */
	@RequestMapping("/query")
	@ResponseBody
	public Object query(Long id) {
		JsonResult<Brand> result = new JsonResult<>();
		Brand brand = brandService.selectById(id);
		result.setData(brand);
		return result.buildIsSuccess(true);
	}

	/**
	 * 修改品牌
	 * 
	 * @param category
	 * @return
	 */
	@RequestMapping("/update")
	@ResponseBody
	public Object update(Brand brand) {
		JsonResult<Brand> result = new JsonResult<>();
		if (brand.getId() == null) {
			return result.buildMsg("新增品牌ID为空").buildIsSuccess(false);
		}
		if(StringUtil.isBlank(brand.getName())) {
			return result.buildMsg("英文品牌不能为空").buildIsSuccess(false);
		}
		brandService.updateBrand(brand);
		return result.buildIsSuccess(true);
	}
	
	/**
	 * 删除品牌
	 *
	 * @param
	 * @return
	 */
	@RequestMapping("/delete")
	@ResponseBody
	public Object delete(Long id) {
		JsonResult<Brand> result = new JsonResult<>();
		brandService.deleteById(id);
		return result.buildIsSuccess(true);
	}

	/**
	 * 分页查询
	 */
	@RequestMapping("/queryBrands")
	@ResponseBody
	public Object queryBrands(BrandQueryVO brandQueryVO) {
		JsonPageResult<List<Brand>> result = new JsonPageResult<>();
		result = brandService.queryBrands(brandQueryVO);
		return result.buildIsSuccess(true);
	}

	/**
	 * 总查询
	 */
	@RequestMapping("/queryAllBrand")
	@ResponseBody
	public Object queryBrandpage(Brand brand) {
		JsonResult<List<Brand>> result = brandService.queryAllBrand();
		return result.buildIsSuccess(true);
	}

}
